//
//  CZTabBar.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
protocol CZTBDelegate: NSObjectProtocol {
    func didSelectedComposeBUtton()
}

class CZTabBar: UITabBar {
    var composeBUttonClouse: (() -> ())?
    
    
    weak var czDelegate: CZTBDelegate?
    
    private lazy var composeButton: UIButton = {
     
        
    let button = UIButton()
        button.addTarget(self, action: #selector(CZTabBar.composeClick), forControlEvents: .TouchUpInside)
        
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_Highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_Highlighted"), forState: .Highlighted)
        button.sizeToFit()
        return button
    }()
    
    
    

    //手写代码的形式去创建对象.不支持 xib/storyBoadr
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    //使用 XIB 或者 storyboard 创建视频的时候会执行
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    
        
        //支持 xib 的写法
//    super.init(coder: aDecoder)
//        setupUI()
    }

    private func setupUI(){
        
        addSubview(composeButton)
    }
    

    
  override  func layoutSubviews(){
        super.layoutSubviews()
        
        composeButton.center.x = frame.size.width * 0.5
        composeButton.center.y = frame.size.height * 0.5
        
        let itemWid = frame.size.width / 5
        
        var index = 0
        
        for chidView in subviews
        {
            if chidView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                chidView.frame.size.width = itemWid
                
                chidView.frame.origin.x = CGFloat(index) * itemWid
                index += 1
                if index == 2 {
                    index += 1
                }
                
            }
        }
        
    }
    

// MARK: ------点击事件------
//使用 private修饰的函数在 swift 运行循环里面是找不到的
    //@objc 告诉系统我们使用 oc 动态运行机制.去调用这个函数
 @objc private func composeClick(){
composeBUttonClouse?()
    //使用代理对 x
    czDelegate?.didSelectedComposeBUtton()
    

    }



}




